<?php namespace app\system\controller;


use houdunwang\request\Request;
use houdunwang\route\Controller;
use houdunwang\validate\Validate;
use system\model\Admin;
use system\model\Config;

class Install extends Controller
{
    //动作
    public function __construct()
    {

    }

//版权信息
    public function copyright()
    {

        //此处书写代码...
        return view();
    }

    public function environmental()
    {
        $data['server_software'] = $_SERVER['SERVER_SOFTWARE'];
        $data['php_version'] = PHP_VERSION;
        $data['pdo'] = extension_loaded('pdo');
        $data['gd'] = extension_loaded('gd');
        $data['curl'] = extension_loaded('curl');
        $data['openssl'] = extension_loaded('openssl');
        return view('', compact('data'));
    }

    public function database()
    {
        if (IS_AJAX) {
            $post = Request::post();
            Validate::make([
                ['host', 'required', '请输入主机地址', Validate::MUST_VALIDATE],
                ['user', 'required', '请输入用户名', Validate::MUST_VALIDATE],
                ['password', 'required', '请输入密码', Validate::MUST_VALIDATE],
                ['database', 'required', '请输入数据库名', Validate::MUST_VALIDATE],
                ['prefix', 'required', '请输入表前缀', Validate::MUST_VALIDATE]
            ]);
//       连接数据库
            $dsn = "mysql:host={$post['host']};dbname={$post['user']}";
            try {
                $pdo = new \PDO($dsn, $post['user'], $post['password']);
                Dir::create('data');
                file_put_contents('data/database.php', "<?php\r\nreturn " . var_export($post, true) . ";\r\n?>");
            } catch (\Exception $exception) {
                return $this->error("数据库连接失败：" . $exception->getMessage());
            }
            return $this->success('数据库连接成功');
        }
        return view();
    }

//数据迁移
    public function tables()
    {
        //创建数据表

        cli('hd migrate:make');


        cli('hd seed:make');
        //写入管理员数据
        $model = new Admin();
        $model['admin_username'] = 'hdcms';
        $model['admin_password'] = '$2y$10$p1znSu9XdfvSk2EBT9L5/OMCEwOL2UXfSBMFpT6/jb5HnPaYVtWPu';
        $model->save();
        $config = new Config();
        $config['weixin'] = '{}';
        $config->save();
        //创建hdjs上传插件需要的附件表
        $sql = <<<sql
CREATE TABLE `wechatcms_attachment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL COMMENT '会员编号',
  `name` varchar(80) NOT NULL,
  `filename` varchar(300) NOT NULL COMMENT '文件名',
  `path` varchar(300) NOT NULL COMMENT '文件路径',
  `extension` varchar(10) NOT NULL DEFAULT '' COMMENT '文件类型',
  `createtime` int(10) NOT NULL COMMENT '上传时间',
  `size` mediumint(9) NOT NULL COMMENT '文件大小',
  `data` varchar(100) NOT NULL DEFAULT '' COMMENT '辅助信息',
  `status` tinyint(1) unsigned NOT NULL COMMENT '状态',
  `content` text NOT NULL COMMENT '扩展数据内容',
  PRIMARY KEY (`id`),
  KEY `data` (`data`),
  KEY `extension` (`extension`),
  KEY `status` (`status`)
) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='附件';
sql;
        Schema::sql($sql);
        go('finish');
    }

    public function finish()
    {
//生成lock.php文件
        touch('lock.php');
//		file_put_contents('lock.php','');
        return view();

    }
}


